home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12016 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  61 lines

  1. Path: news.channel1.com!channel1!dspse.bedford
  2. Distribution: world
  3. Newsgroups: comp.lang.c
  4. Subject: Dynamic 2-D arrays
  5. From: dspse.bedford@channel1.com (Dspse Bedford)
  6. Message-ID: <40.69871.1612@channel1.com>
  7. Date: Thu, 28 Mar 1996 06:28:00 -0640
  8. Organization: Channel 1(R) 617-864-0100 Info
  9.  
  10. I want to define a 2-D array through a pointer-to-pointer structure member
  11. as shown below:
  12.  
  13. typedef struct
  14. { float **matrix;}Obj;
  15. main()
  16. {Obj *m;
  17.  int x=5;y=10;
  18.  m=(Obj *)malloc(sizeof(Obj));
  19.  *(m->matrix)=(float *)malloc(x*y*sizeof(float *));
  20.   ...
  21.  ...
  22.  return;}
  23.  
  24. if I index in the following way: m->matrix[i][j], how does it increment the 
  25. pointer to determine the memory location?  If I have 
  26. m->matrix[1][2] and m->matrix[2][1] what is the difference?
  27.  
  28. In the past, I would use something like whats below, but the difference is 
  29. I do now know ahead of time the Y_dimension or the X_dimension.
  30.  
  31. #define Y_DIM 10
  32. #define X_DIM  5
  33. typedef struct
  34. {float *matrix[Y_DIM];}Obj;
  35. main()
  36. {
  37.   Obj *m;
  38.   m=(Obj *)malloc(sizeof(Obj));
  39.   for (i=0;i<Y_DIM;++i)
  40.   { m->matrix[i]=(float *)malloc(X_DIM*sizeof(float));}
  41.  ..
  42.  return;}
  43.  
  44. The above ofcourse indexes fine since I create an array of pointers of the 
  45. proper Y-dimension in my structure definition.
  46.  
  47. Any help would be appreciated.
  48.  
  49. Thank you,
  50. Anastasios Maurudis
  51. anastasios@dspse.com
  52. DSP Software Engineering, Inc.
  53. 175 Middlesex Turnpike
  54. Bedford, MA 01730
  55. (617)275-3733
  56. (617)275-4323 Fax
  57.    
  58.  
  59. ---
  60.  * WR 1.32 # 331 * Blue Wave - what Smurfs do at a football game..
  61.